package com.hutu.kboshi.dao;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hutu.kboshi.common.ErrorCode;
import com.hutu.kboshi.common.constant.CommonConstant;
import com.hutu.kboshi.common.exception.ThrowUtils;
import com.hutu.kboshi.mapper.QuestionMapper;
import com.hutu.kboshi.pojo.dto.question.QuestionQueryRequest;
import com.hutu.kboshi.pojo.entity.Question;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Repository
public class QuestionDao extends ServiceImpl<QuestionMapper, Question> implements IService<Question> {

    public Page<Question> pages(QuestionQueryRequest questionQueryRequest) {
        long current = questionQueryRequest.getCurrent();
        long size = questionQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<Question> page = new Page<>(current, size);
        LambdaQueryWrapper<Question> queryWrapper = getQueryWrapper(questionQueryRequest);
        return baseMapper.selectPage(page,queryWrapper);
    }

    public LambdaQueryWrapper<Question> getQueryWrapper(QuestionQueryRequest questionQueryRequest) {
        LambdaQueryWrapper<Question> queryWrapper = new LambdaQueryWrapper<>();
        String title = questionQueryRequest.getTitle();
        Integer difficultyLevel = questionQueryRequest.getDifficultyLevel();
        String sortField = questionQueryRequest.getSortField();
        String sortOrder = questionQueryRequest.getSortOrder();
        List<String> tagList = questionQueryRequest.getTags();
        // JSON 数组查询
        if (CollUtil.isNotEmpty(tagList)) {
            for (String tag : tagList) {
                queryWrapper.like(StringUtils.isNotBlank(tag), Question::getTags,tag);
            }
        }
        queryWrapper.like(StringUtils.isNotBlank(title), Question::getTitle, title)
                .eq(difficultyLevel != null, Question::getDifficultyLevel, difficultyLevel)
                .orderBy(StringUtils.isNotBlank(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),Question::getCreateTime);
        return queryWrapper;
    }

    public void batchDeleteQuestionByIds(List<Long> questionIdList) {
        if (CollectionUtil.isNotEmpty(questionIdList)){
            baseMapper.deleteBatchIds(questionIdList);
        }
    }

    public List<Long> findQuestionIdsByTag(String tag) {
        return lambdaQuery().select(Question::getId).like(Question::getTags, tag).list().stream() // 转换为流
                .map(Question::getId) // 映射到 id
                .collect(Collectors.toList()); // 收集为 List<Long>
    }
}